import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.TableModel;


public class main {

	/**
	 * This will print the results to the screen. It takes a resultset from a query you have performed
	 * Caution currently doesn't handle exceptions. 
	 * @param result
	 * @throws SQLException
	 */
	public static void printResults(ResultSet result) throws SQLException{
		ResultSetMetaData r = result.getMetaData();
		int colCount = r.getColumnCount();                                    
		// Find number of columns in EMP
		while(result.next()){
			for (int i=1; i<= colCount; i++) {                                          
				String colName = r.getColumnName(i);    
				System.out.print(colName);
				System.out.print(" " + result.getString(i)+" 	");
			}
			System.out.println();
		}
	}
	/**
	 * Sends a string that contains a query to the server, prints the results, and closes the stream.
	 * @param the_query your SQL statement
	 */
	public static void Query(String the_query){

		try {
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://sql2.freesqldatabase.com:3306/sql28853", "sql28853", "uQ2*iN8%");
			System.err.print("Stream opened");
			Statement stat = conn.createStatement();
			//Change this line to execute a particular query
			ResultSet result = stat.executeQuery(the_query);
			printResults(result);
			conn.close();
			System.err.print("Stream closed");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}


	public static void main(String[] args) throws SQLException{
		
		final blendPanel blends = new blendPanel();
		Connection conn = DriverManager.getConnection(
				"jdbc:mysql://sql2.freesqldatabase.com:3306/sql28853", "sql28853", "uQ2*iN8%");
		final String[] values= new String[10];
		TableModel mod =
				new GenericTableModel (conn, "blend");
		JTable jtable = new JTable (mod);
		jtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jtable.addMouseListener(new MouseAdapter() { 
			public void mousePressed(MouseEvent me) { 
				JTable r = ((JTable) me.getSource());
				for(int i =0; i<=r.getColumnCount()-1;i++){
					System.out.println(r.getModel().getValueAt(r.getSelectedRow(), i));  
					values[i]= r.getModel().getValueAt(r.getSelectedRow(), i).toString();
				}
				blends.setFields(values[0], values[1], values[2], values[3], values[4], values[5], values[6]);
			}
		}); 

		JScrollPane scroller =
				new JScrollPane (jtable, 
						ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, 
						ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		JFrame frame = new JFrame ("Test");
		frame.getContentPane().setLayout(new BorderLayout());
		frame.getContentPane().add (scroller, BorderLayout.NORTH);
		frame.getContentPane().add(blends,BorderLayout.SOUTH);
		frame.pack();
		frame.setVisible (true);

		conn.close();


	}


}

